package cn.z2huo.demo.redis.spring.service.impl;

import cn.z2huo.demo.redis.spring.service.api.ValueOperationsService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * <p>这个方式是为了避免写 template.opsForXXX 方法
 * <p>但是出现了乱码
 *
 * @author z2huo
 */
@Service
public class ValueOperationsServiceImpl implements ValueOperationsService {

//    @Autowired
//    private RedisOperations<String, String> operations;

//    @Autowired
//    @Qualifier("stringRedisTemplate")
//    @Autowired(required = false)
//    @Qualifier("redisTemplate")
    @Resource(name="redisTemplate")
    private ValueOperations<String, String> valueOperations;

    @Override
    public void set(String key, String value) {
        if (StringUtils.isBlank(key)) {
            throw new RuntimeException("key is null");
        }
        valueOperations.set(key, value);
    }

    @Override
    public String get(String key) {
        return valueOperations.get(key);
    }
}
